import Vue from 'vue'
const api = window.$api.data.dbsource

let DataSourceLoadTimer = {} // 加载用户信息定时器
const DataSourceLoadings = {}  // 需要加载的用户信息

window.dataGroup = {}
window.dataByParamterGroup = {}
window.dataByShowAll = {}

const dataSource = {
    namespaced: true,
    state: {
        list: [],
        isUpdate: false,

        dataGroup: {},
        colNameGroup: {},
        dataByParamterGroup: {},
        dataByShowAll : {},

        updateStateList: {},
        updateStateListForCol: {},
        updateStateListForParam: {},
        updateStateListForValue: {},

        dataMap: {}
    },
    actions: {
        clear({ commit }) {
            commit('CLEAR')
        },
        async loadList({ commit, state }, isCache) {
            if (!state.isUpdate || !isCache) {
                state.isUpdate = true
                const res = await api.getList()
                const data = res.data.data
                commit('SET_DATASOURCE_LIST', data)
            }
        },
        async loadDataCol({ commit, state }, { code, isCache }) {
            if (!state.updateStateListForCol[code] || !isCache) {
                state.updateStateListForCol[code] = true
                const res = await api.getColNameList(code)
                const data = res.data.data
                commit('SET_DATASOURCE_COLNAMEGROUP', { code, data })
            }
        },
        async loadDataList({ commit, state }, { code, isCache }) {
            if (!state.updateStateList[code] || !isCache) {
                state.updateStateList[code] = true
                const res = await api.getDataList(code)
                const data = res.data.data
                commit('SET_DATASOURCE_DATAGROUP', { code, data })
            }
        },
        async loadDataListByShowAll({ commit, state }, { code, isCache }) {
            if (!state.updateStateListForParamShowAll[code] || !isCache) {
                state.updateStateListForParamShowAll[code] = true
                const res = await api.getDataListByParamter(code, '')
                const data = res.data.data
                commit('SET_DATASOURCE_DATABYSHOWALL', { code, data })
            }
        },
        async loadDataListByParamter({ commit, state }, { code, param, isCache }) {
            const codeParam = `${code}_${param}`
            if (!state.updateStateListForParam[codeParam] || !isCache) {
                state.updateStateListForParam[codeParam] = true
                const res = await api.getDataListByParamter(code, param)
                const data = res.data.data
                commit('SET_DATASOURCE_DATABYPARAMTERGROUP', { codeParam, data })
            }
        },
        async loadDataListByValue({ commit, state }, { code, fieldName, values }) {
            DataSourceLoadings[code] = DataSourceLoadings[code] || []
            values = (values || '') + ''
            const vList = values.split(',')
            vList.forEach(value => {
                const key = `${code}_${value}`
                if (!state.updateStateListForValue[key] && !state.dataMap[key]) {
                    state.updateStateListForValue[key] = true
                    DataSourceLoadings[code].push(value)
                }
            })

            if (DataSourceLoadings[code].length > 0 && !DataSourceLoadTimer[code]) {
                DataSourceLoadTimer[code] = setTimeout(async () => {
                    if (DataSourceLoadings[code].length > 0) {
                        const ids = String(DataSourceLoadings[code])
                        DataSourceLoadings[code].length = 0

                        var res = await api.getDataListByValues(code, fieldName, { ids: ids })
                        const data = res.data.data || []
                        data.forEach(item => {
                            commit('SET_DATASOURCE_ONE', { codeValue: `${code}_${item[fieldName]}`, data: item })
                        })
                    }
                    DataSourceLoadTimer[code] = null
                }, 50)
            }
        }
    },
    mutations: {
        CLEAR: (state) => {
            state.isUpdate = false;
            state.updateStateList = {};
            state.updateStateListForCol= {};
            state.updateStateListForParam= {};
            state.updateStateListForValue= {};
            state.updateStateListForParamShowAll= {};
            state.dataMap= {};

            window.dataGroup = {}
            window.dataByParamterGroup = {}
            window.dataByShowAll = {}

            state.list = [];

            state.dataGroup= {};
            state.colNameGroup= {};
            state.dataByParamterGroup= {};
            state.dataByShowAll= {};
    
        },
        SET_DATASOURCE_LIST: (state, payload) => {
            state.list = payload;
        },
        SET_DATASOURCE_DATAGROUP: (state, payload) => {
            window.dataGroup[payload.code] = payload.data
            Vue.set(state.dataGroup, payload.code, window.$uuid())
        },
        SET_DATASOURCE_COLNAMEGROUP: (state, payload) => {
            Vue.set(state.colNameGroup, payload.code, payload.data)
        },
        SET_DATASOURCE_DATABYPARAMTERGROUP: (state, payload) => {
            window.dataByParamterGroup[payload.codeParam] = payload.data
            Vue.set(state.dataByParamterGroup, payload.codeParam, window.$uuid())
        },
        SET_DATASOURCE_DATABYSHOWALL: (state, payload) => {
            window.dataByShowAll[payload.code] = payload.data
            Vue.set(state.dataByShowAll, payload.code, window.$uuid())
        },
        SET_DATASOURCE_ONE: (state, payload) => {
            Vue.set(state.dataMap, payload.codeValue, payload.data)
        },
    }
}

export default dataSource;